<?php
namespace app\model;

use think\Model;
use app\model\NavSite;

/**
 * 导航分类
 */
class NavSiteType extends Model
{

    protected $table = "ea_nav_site_type";

    /**
     * [setWhere 过滤条件]
     * @since  2022-03-18
     * @author Dai哥
     * @param  [type]     $data [description]
     */
    public static function setWhere($data)
    {
        $model =  self::whereNull('delete_time')->where('status', 1)->order('sort ASC');

        return $model;
    }

    /**
     * [getTypeList 获取分类列表]
     * @since  2022-03-18
     * @author Dai哥
     * @return [type]     [description]
     */
    public static function getTypeList($data = [])
    {
        $list = self::setWhere($data)->field('id, name')->select();

        return $list;
    }

    /**
     * [getTypeListArr 获取导航列表，通过关联模型获取详细的数据]
     * @since  2022-03-18
     * @author Dai哥
     * @param  array      $data [description]
     * @return [type]           [description]
     */
    public static function getTypeListArr($data = [])
    {
        $list = self::setWhere($data)
            ->field('id, name')
            ->order('sort ASC')
            ->with(['site' => function($query) use ($data){
                $query->whereNull('delete_time')->field('id, title, url, pic, type_id, descs')->order('sort ASC');
            }])
            ->select();

        return $list;
    }

    /**
     * [site 关联查出下面所有的导航信息]
     * @since  2022-03-18
     * @author Dai哥
     * @return [type]     [description]
     */
    public function site()
    {
        return $this->hasMany(NavSite::class, 'type_id', 'id');
    }

}